<?php
/**
 * Created by inline.
 * User: swb
 * Date: 2022-11-10
 * Time: 01:11
 */
namespace app\store\controller\api\v1;
use library\Controller;
use app\common\controller\BaseApiController;
use app\common\facade\Token;
use app\common\model\ShopGoods;
use library\tools\Data;
use app\common\model\ShopOrder;
use app\common\model\ShopOrderGoods;
use app\common\model\Account;

class OffLine extends BaseApiController
{
    //选择商品
    public function getOffLineGoods(){
        $ids01 = $this->request->param('ids');
        $ids = explode(',', $ids01);
        $goods = [];
        for ($i = 0; $i < count($ids); $i++) {
            $OLGood = ShopGoods::where('id',$ids[$i])->find();
            $goods[$i] = $OLGood;
        }
        $this->successCode($goods);
    }
    
    public function postOffLineOrder(){
        $store_id = $this->request->param('store_id',0);
        $shop_ids = $this->request->param('shop_ids');
        $list_um = $this->request->param('list_num');
        if($store_id == 0){
            $this->errorCode(100,'无法绑定店铺，请查询登录');
        }elseif (count($shop_ids) == 0){
            $this->errorCode(100,'请选择商品，商品id为空');
        }elseif(count($list_um) == 0){
            $this->errorCode(100,'请添加数量，数量为0');
        }

        //提交的商品id
        $goods = ShopGoods::whereIn('id', $shop_ids)->select();
        
        $order_sn = Data::uniqidDateCode();//生成order_no
        $visable = false;
        $price_express = 0;//快递费
        $goods_price = 0;
        $goods_num = 0;
        //商品标题
        $title = '';
        if (count($goods) > 1) {
            $visable = true;
        }else{
            $title = $goods[0]['title'].$goods[0]['num']."件";
        }
        foreach ($goods as $key => $data) {
            $goods_price += $data['price'] * $list_um[$key];
            $goods_num += $list_um[$key];
            if ($visable){
                $title .= $data['title'].$list_um[$key].'件/';
            }
        }
        if ($visable){
            $data['title'] = substr($data['title'],0,-1);
        }
        //订单数据组装
        $orderData = [
            'title' => $title,
            'type' => 1,
            'store_id' => $store_id?$store_id:0,
            'order_no' => $order_sn,
            'freight_price' => $price_express,
            'buy_num' => $goods_num,
            'goods_price' => $goods_price,
            'pay_price' => $goods_price,
            'total_price' => $goods_price,
            'express_type' => 2,
            'pay_type' => 2,
            'is_pay' => 1,
            'pay_time' => date('Y-m-d H:i:s'),
            'status' => 2
        ];
        $order = ShopOrder::create($orderData);
        if ($order){
            $orderGoods = [];
            foreach ($goods as $key => $data) {
                //快照组装
                $orderGoods[] = [
                    'order_id' => $order['id'],
                    'good_id' => $data['id'],
                    'good_title' => $data['title'],
                    'good_logo' => $data['logo'],
                    'price' => $data['price'],
                    'num' => $list_um[$key]
                ];
            }
            (new ShopOrderGoods())->saveAll($orderGoods);
            Account::record($goods_price, 0,"{$order['title']}",1,0,"",$store_id,$order['id']);
        }
        $this->successCode();
    }
}